package com.echo.code.thread.mode.worker;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * Created by baiyap on 2018/8/8
 */
public class Worker extends Thread {

  private final static ThreadLocalRandom random = ThreadLocalRandom.current();
  private final Channel channel;

  public Worker(Channel channel) {
    this.channel = channel;
  }

  @Override
  public void run() {
    while (true) {
      try {
        Work work = channel.takeWork();
        work.workout();
        TimeUnit.SECONDS.sleep(random.nextInt(10));
      } catch (InterruptedException e) {
      }
    }
  }
}

